package edu.hawaii.myisern.action;

import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.net.MalformedURLException;
import org.junit.Before;
import org.junit.Test;
import org.xml.sax.SAXException;
import com.meterware.httpunit.WebConversation;
import com.meterware.httpunit.WebForm;
import com.meterware.httpunit.WebRequest;
import com.meterware.httpunit.WebResponse;

/**
 * Test Class for login and logout.
 * @author Shaoxuan
 *
 */
public class TestLogin {
  private final String MAIN_FRAME = "main";
  /** Get the test host. */
  //private String testHost = "http://localhost:8080/";
  private String testHost = "http://localhost:8080/";
  private WebConversation conversation = new WebConversation();
  private final String HEAD_FRAME = "navigation";
  private final String USERNAME = "admin";

  /**
   * Initialize a web conversation.
   * @throws MalformedURLException java.net.MalformedURLException
   * @throws IOException java.io.IOException
   * @throws SAXException thrown if there is an error parsing this response
   */
  @Before public void setUp() throws MalformedURLException, IOException, SAXException {
    // Get welcome.jsp page and check for successful retrieval
    String Url = testHost + "myisern";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("See if get the right page", "Login", response.getTitle());
    //login
    WebForm form = response.getFormWithID("LoginForm");
    WebRequest request = form.getRequest();
    request.setParameter("username", USERNAME);
    request.setParameter("password", USERNAME);
    response = conversation.getResponse(request);
    assertEquals("Check if login succeeded.", 2,response.getFrameNames().length);
    response = response.getSubframeContents(HEAD_FRAME);
    request = response.getLinkWith("Logout").getRequest();
    response = this.conversation.getResponse(request);
  }
  
  /**
   * Initialize a web conversation.
   * @throws MalformedURLException java.net.MalformedURLException
   * @throws IOException java.io.IOException
   * @throws SAXException thrown if there is an error parsing this response
   */
  @Test public void testLoginAndLogout() throws MalformedURLException, IOException, SAXException {
    // Get welcome.jsp page and check for successful retrieval
    String Url = testHost + "myisern/myisern.jsp";
    WebResponse response = conversation.getResponse(Url);
    assertEquals("See if get the right page", "Logout", response.getTitle());
    WebRequest request = response.getLinkWith("login").getRequest();
    response = this.conversation.getResponse(request);
    assertEquals("See if get the right page", "Login", response.getTitle());
    //login
    WebForm form = response.getFormWithID("LoginForm");
    request = form.getRequest();
    request.setParameter("username", USERNAME);
    request.setParameter("password", USERNAME);
    response = conversation.getResponse(request);
    assertEquals("Check if login succeeded.", 2,response.getFrameNames().length);
  }
}
